﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebTemplate.DAL.Entities;
using System.Configuration;
using System.Data.SqlClient;

namespace WebTemplate.DAL.Repositories
{
    public class StaticRepository
    {
        public void Insert(AnyStaticPage Item)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[StaticContent_Insert]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@Content" , Item.Content);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public List<AnyStaticPage> Select(StaticSearchCriteria Item)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[StaticContent_Select]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@Id", Item.Id);
            command.Parameters.AddWithValue("@Content", Item.Content);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();
            List<AnyStaticPage> reg = new List<AnyStaticPage>();

            while (reader.Read())
            {
                AnyStaticPage obj = new AnyStaticPage();

                obj.Id = reader.GetInt32(0);
                obj.Content = reader.GetString(1);
                reg.Add(obj);
            }

            connection.Close();

            return reg;

            
        }
    }
}
